﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Common.Helpers
{
	public class GeoHelper
	{
		private const double EarthRadiusKm = 6371; // 地球半径，单位为千米

		public static double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
		{
			var dLat = ToRadians(lat2 - lat1);
			var dLon = ToRadians(lon2 - lon2);

			lat1 = ToRadians(lat1);
			lat2 = ToRadians(lat2);

			var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
					Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
			var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
			var dist = EarthRadiusKm * c;

			return dist;
		}

		private static double ToRadians(double angle)
		{
			return angle * (Math.PI / 180);
		}

	}
}
